package com.bawei.mdm2207alast.mine.ask

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.mdm2207alast.R
import com.bawei.mdm2207alast.base.OnError
import com.bawei.mdm2207alast.base.OnLoading
import com.bawei.mdm2207alast.base.OnSuccess
import com.bawei.mdm2207alast.common.BaseTitle
import com.bawei.mdm2207alast.model.db.AskEntity
import com.bawei.mdm2207alast.route.RouteConfig
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import kotlinx.coroutines.launch

/**
 * 作者：赵亮
 * 时间：2024/12/24 16:23
 * 功能：审批列表页面
 */
@Composable
fun AskApproveListPage(
    navController: NavController = rememberNavController(),
    viewModel: AskViewModel = hiltViewModel()
) {

    // 数据
    val list = remember {
        SnapshotStateList<AskEntity>()
    }
    LaunchedEffect(key1 = "") {
        launch {
            viewModel.uiState.collect {
                when (it) {
                    is OnError -> {
                        ToastUtils.showShort("查询失败")
                    }

                    OnLoading -> {}
                    is OnSuccess<*> -> {
                        list.clear()
                        list.addAll(it.data as Collection<AskEntity>)
                    }
                }
            }
        }
        launch {
            viewModel.sendIntent(AskIntent.SelAskNoApprove)
        }
    }

    Column {
        BaseTitle(navController = navController, title = "待审核")

        // 待审核列表
        Column(modifier = Modifier.padding(start = 15.dp, end = 15.dp)) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(color = colorResource(id = R.color.ask_list_title_bg))
                    .height(50.dp),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically

            ) {
                Text(text = "类型", modifier = Modifier.padding(start = 15.dp))
                Text(text = "开始时间")
                Text(text = "结束时间")
                Text(text = "审批状态", modifier = Modifier.padding(end = 15.dp))
            }
            LazyColumn(
                modifier = Modifier.padding(top = 10.dp),
                verticalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                items(list) {
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.SpaceBetween,
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(50.dp)
                            .clickable {
                                // 跳转到审批页面
                                navController.navigate(
                                    RouteConfig.ASK_APPROVE + "/${
                                        Gson().toJson(
                                            it
                                        )
                                    }"
                                )
                            }
                            .background(
                                color = colorResource(
                                    id = R.color.add_report_bg
                                ), shape = RoundedCornerShape(10.dp)
                            )
                    ) {
                        Text(text = it.type, modifier = Modifier.padding(start = 10.dp))
                        Text(text = it.start)
                        Text(text = it.end)
                        Text(text = it.status, modifier = Modifier.padding(end = 10.dp))
                    }
                }
            }
        }
    }
}

@Preview(showSystemUi = true)
@Composable
fun AskApproveListPagePreview() {
    AskApproveListPage()
}

